package com.hsbc.dev.teamo4.sms.sm.dao.impl;

import com.google.common.collect.Lists;
import com.hsbc.dev.teamo4.sms.common.utils.data.DBUtil;
import com.hsbc.dev.teamo4.sms.sm.dao.IPermissionDao;
import com.hsbc.dev.teamo4.sms.sm.po.Permission;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @author:Kevin
 * @version:
 * @Project: permission_manage
 * @Package: com.hsbc.dev.teamo4.sms.sm.dao.impl
 * @Description:
 * @Date date: 2018/7/26
 */
public class PermissionDaoImpl implements IPermissionDao{
    /**
     * save Entity
     * @param permission
     * @return
     * @throws SQLException
     */
    @Override
    public Permission saveEntity(Permission permission)  {
        String sql="insert into sm_permission values(?,?,?,?,?,?,?,?)";
        return (Permission) DBUtil.saveEntity(sql,permission,null);
    }

    @Override
    public Permission updateEntity(Permission permission)  {
        String sql="update  sm_permission set  permissionName=?,permissionHref=?,updateId=?,updateTime=?,isEnable=? where id=?";
        return (Permission) DBUtil.updateEntity(sql,permission,Lists.newArrayList("id","createId","createTime"),permission.getId());
    }

    @Override
    public int updateByIsEnable(String id, Boolean bool) {
        String sql = "update sm_permission set isEnable=? where id=?";
        return DBUtil.updateByIsEnable(sql,id,bool);
    }

    /**
     *query By Id
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public Permission queryById(String id){
        String sql="select * from sm_permission where id='"+id+"'";
        BeanHandler beanHandler=new BeanHandler<>(Permission.class);
        return (Permission) DBUtil.queryById(sql,beanHandler);
    }
    /**
     * query All Entity
     * @return
     * @throws SQLException
     */
    @Override
    public List<Permission> queryAllEntity(){
        String sql="select * from sm_permission where isEnable='"+true+"'";
        BeanListHandler beanListHandler=new BeanListHandler<>(Permission.class);
        return (List<Permission>) DBUtil.queryAllEntity(sql,beanListHandler);
    }

    /**
     * countByNameAndParentId
     * @param menuId
     * @param permissionName
     * @param permissionId
     * @return Map
     */
    @Override
    public Map countByNameAndParentId(String menuId, String permissionName, String permissionId) {
        String sql="select count(1) from sm_permission where menuId='"+menuId+"' and permissionName= '"+permissionName+"' and id= '"+permissionId+"'";
        MapHandler mapHandler=new MapHandler();
        return (Map) DBUtil.countAndToMap(sql,mapHandler);
    }

    /**
     * queryByMenuId
     * @param menuId
     * @return List<Permission>
     */
    @Override
    public List<Permission> queryByMenuId(String menuId) {
        String sql ="select * from sm_permission where menuId='"+menuId+"'";
        BeanListHandler beanListHandler=new BeanListHandler<>(Permission.class);
        return (List<Permission>) DBUtil.queryAllEntity(sql,beanListHandler);
    }

    /**
     * getPermissionByIdList
     * @param list
     * @return List<Permission>
     */
    @Override
    public List<Permission> getPermissionByIdList(List<String> list) {
       List<Permission> permissionList=Lists.newArrayList();
        for (String id:list){
            Permission permission=queryById(id);
            if(permission!=null){
                permissionList.add(permission);
            }
        }
        return permissionList;
    }
}
